package com.xmy.manager;

import com.xmy.entity.Book;
import com.xmy.service.BookService;
import com.xmy.service.impl.BookServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class BookManager {
    Scanner input = new Scanner(System.in);
    BookService bs = new BookServiceImpl();

    /**
     * 主菜单
     */
    public void menu(){
        System.out.println("---图书管理系统---");
        do {
            System.out.println("1.查看");
            System.out.println("2.新增");
            System.out.println("3.修改");
            System.out.println("4.删除");
            System.out.println("5.退出");
            System.out.print("请选择：");
            int choose = input.nextInt();
            switch (choose){
                case 1:
                    findBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    updateBook();
                    break;
                case 4:
                    delBook();
                    break;
                case 5:
                    System.out.println("谢谢使用!系统退出！");
                    System.exit(0);
                    break;
            }
        } while (true);
    }

    /**
     * 删除
     */
    private void delBook() {
        System.out.print("请输入要删除的图书编号：");
        int id = input.nextInt();
        bs.delBook(id);
        System.out.println("删除成功!");
    }

    /**
     * 修改
     */
    private void updateBook() {
        System.out.print("请输入要修改的图书编号：");
        int id = input.nextInt();
        Book book = new Book();
        System.out.print("请输入图书类别(1.计算机 2.小说 3.文学 4.杂项)：");
        book.setTypeId(input.nextInt());
        System.out.print("请输入图书名称");
        book.setTitle(input.next());
        System.out.print("请输入图书摘要：");
        book.setSummary(input.next());
        System.out.print("请输入上传人：");
        book.setUploaduser(input.next());
        System.out.print("请输入上传时间：");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(input.next());
            book.setCreatedate(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        bs.updateBook(book,id);
        System.out.println("修改成功！");
    }

    /**
     * 新增
     */
    private void addBook() {
        Book book = new Book();
        System.out.print("请输入图书类别(1.计算机 2.小说 3.文学 4.杂项)：");
        book.setTypeId(input.nextInt());
        System.out.print("请输入图书名称");
        book.setTitle(input.next());
        System.out.print("请输入图书摘要：");
        book.setSummary(input.next());
        System.out.print("请输入上传人：");
        book.setUploaduser(input.next());
        System.out.print("请输入上传时间：");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(input.next());
            book.setCreatedate(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        bs.addBook(book);
        System.out.println("新增成功！");
    }

    /**
     * 查看
     */
    private void findBook() {
        System.out.print("请输入每页的条数：");
        int size = input.nextInt();
        System.out.println("总页数是："+(bs.selCount()%size == 0?bs.selCount()/size:(bs.selCount()/size+1)));
        System.out.print("请输入您要查看的页数：");
        int page = input.nextInt();
        List<Book> books = bs.SelAllBook(page,size);
        System.out.println("图书编号\t类别编号\t图书名称\t图书摘要\t上传人\t上传时间");
        for (Book book:books) {
            System.out.println(book.getId()+"\t"+book.getTypeId()+"\t"+book.getTitle()+"\t"+book.getSummary()+"\t"+book.getUploaduser()+"\t"+book.getCreatedate());
        }
    }
}
